﻿Shader "WastelandWaves/HDRP/Test.WWTerrain"
{
    Properties
    {
        _Scale("Scale", Vector) = (1, 1, 1, 1)
        _Translate("Translate", Vector) = (0, 0, 0, 0)
        _HeightMap("HeightMap", 2D) = "black" { }
    }

        HLSLINCLUDE
        
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/Lighting.hlsl"
/*
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl"

//-------------------------------------------------------------------------------------
// variable declaration
//-------------------------------------------------------------------------------------

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitProperties.hlsl"
*/
// This is a test shader for Wasteland Waves Terrain System
// #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Macros.hlsl"
#define SHADERGRAPH_PREVIEW 1

CBUFFER_START(UnityPerMaterial)
CBUFFER_END

TEXTURE2D(_HeightMap); SAMPLER(sampler_HeightMap);
    float4 _Scale;
    float4 _Translate;

    float2 DisplaceUV(float2 tex, float4 dis) {
        return ((tex.xy) * dis.xy + dis.zw);
    }

    struct GraphVertexInput
    {
        float4 vertex : POSITION;
        float4 texcoord0 : TEXCOORD0;
        UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    ENDHLSL

        SubShader
    {
        Tags {
        "RenderPipeline" = "HDRenderPipeline"
        "RenderType" = "HDUnlitShader"
        "RenderType" = "Opaque" }
        LOD 100

        Pass
        {
            HLSLPROGRAM
            /*
            #define SHADERPASS SHADERPASS_FORWARD_UNLIT
            #define ATTRIBUTES_NEED_TEXCOORD0

            // #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl"

            #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Material.hlsl"
            #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.hlsl"
            #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/ShaderPass/UnlitSharePass.hlsl"
            #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/UnlitData.hlsl"
            #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassForwardUnlit.hlsl"
            */
            #pragma vertex vert
            #pragma fragment frag
            
            struct appdata
            {
                float4 vertex: POSITION;
                float3 normal: NORMAL;
                float2 uv: TEXCOORD0;
                
            };

            struct v2f
            {
                float4 pos: SV_POSITION;
                float3 posWS: TEXCOORD0;
                float3 normal: TEXCOORD1;
                float2 uv: TEXCOORD2;
                
            };

            // AttributesMesh 定义在 hdrp...\RenderPipeline\ShaderPass\VaryingMesh.hlsl
            // PackedVaryingsType 是传给默认 frag shadder 的 
        /*
            PackedVaryingsType vert(AttributesMesh inputMesh)
            {
                inputMesh.positionOS.y += SAMPLE_TEXTURE2D_LOD(_HeightMap, sampler_HeightMap, inputMesh.uv0, 0.0).r;

                VaryingsType varyingsType;
                varyingsType.vmesh = VertMesh(inputMesh);
                return PackVaryingsType(varyingsType);
            }*/

            v2f vert(appdata i) {
                v2f o;
                float4 vp = i.vertex;
                //vp.y += SAMPLE_TEXTURE2D_LOD(_HeightMap, sampler_HeightMap, i.uv, 0.0).r;
                float3 positionWS = TransformObjectToWorld(vp.xyz);
                o.pos = TransformWorldToHClip(positionWS);
                o.uv = i.uv;
                o.normal = TransformObjectToWorldNormal(i.normal);
                o.posWS = vp.xyz;//positionWS;
                //o.pos.y += SAMPLE_TEXTURE2D_LOD(_HeightMap, sampler_HeightMap, i.uv, 0.0).r;
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                DirectionalLightData light = _DirectionalLightDatas[0];
                float3 normal = i.normal;
                float3 lightDir = -light.forward.xyz;
                float3 viewDir = normalize(GetWorldSpaceNormalizeViewDir(i.posWS));
                float3 lightColor = light.color.rgb;

                float3 halfDir = normalize(lightDir + viewDir);
                float _Gloss = 0.2f;
                float4 _Specular = float4(1.0f, 1.0f, 1.0f, 1.0f) * 0.0001;
                float3 specular = lightColor * _Specular.rgb * pow(max(0, dot(normal, halfDir)), _Gloss);
                // return float4(i.normal.rgb * 0.5 + 0.5, 1.f);
                float sca = i.posWS.y * 2;
                float3 color = lerp(float3(0.01f, 0.02f, 1.f),float3(1.f, 0.01f, 0.02f), sca);

                
                return float4(specular* 0.6 + color, 1.f) + 0.01 * 0.01 * float4(0.4f, 0.7f, 0.1f, 0.f);
            }
            ENDHLSL
        }
    }
}
